package com.mi.patient;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class patient {
    public static class patientMapper extends Mapper<LongWritable, Text, Text, FloatWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, FloatWritable>.Context context) throws IOException, InterruptedException {
            if (key.get() == 0) return;
            String line = value.toString();
            String[] fields = line.split(",");
            // 药品费自费金额
            String drugOwnExpense = fields[31];
            // 检查费自费金额
            String checkOwnExpense = fields[37];
            // 治疗费自费金额
            String treatmentOwnExpense = fields[41];
            // 手术费自费金额
            String surgeryOwnExpense = fields[44];
            // 医用材料费自费金额
            String medicalOwnExpense = fields[50];
            // 输出
            context.write(new Text("药品费自费金额"), new FloatWritable(Float.parseFloat(drugOwnExpense)));
            context.write(new Text("检查费自费金额"), new FloatWritable(Float.parseFloat(checkOwnExpense)));
            context.write(new Text("治疗费自费金额"), new FloatWritable(Float.parseFloat(treatmentOwnExpense)));
            context.write(new Text("手术费自费金额"), new FloatWritable(Float.parseFloat(surgeryOwnExpense)));
            context.write(new Text("医用材料费自费金额"), new FloatWritable(Float.parseFloat(medicalOwnExpense)));
        }
    }

    public static class patientReducer extends Reducer<Text, FloatWritable, Text, FloatWritable> {
        private Map<String, Float> avgAmounts = new HashMap<>();
        private float avgAmountsSum;

        @Override
        protected void reduce(Text key, Iterable<FloatWritable> values, Reducer<Text, FloatWritable, Text, FloatWritable>.Context context) throws IOException, InterruptedException {
            float sum = 0;
            int count = 0;
            for (FloatWritable val : values) {
                sum += val.get();
                count++;
            }
            float average = sum / count;
            avgAmounts.put(key.toString(), average);
            avgAmountsSum += average;
        }

        @Override
        protected void cleanup(Reducer<Text, FloatWritable, Text, FloatWritable>.Context context) throws IOException, InterruptedException {
            ArrayList<Map.Entry<String, Float>> list = new ArrayList<>(avgAmounts.entrySet());
            list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
            for(Map.Entry<String, Float> amount:list){
                context.write(new Text("平均" + amount.getKey() + "占比"), new FloatWritable(amount.getValue() / avgAmountsSum));
            }
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        // 创建配置类
        Configuration config = new Configuration();
        // 创建Job作业对象
        Job job = Job.getInstance(config);
        // 指定Job对象所属的类
        job.setJarByClass(patient.class);
        // 指定Mapper
        job.setMapperClass(patientMapper.class);
        // 指定Mapper输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FloatWritable.class);
        // 指定Reducer
        job.setReducerClass(patientReducer.class);
        // 指定Reducer输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FloatWritable.class);
        FileInputFormat.setInputPaths(job, new Path("data/data.csv"));
        // 指定处理结果的输出路径
        Path outputPath = new Path("output");
        FileSystem fs = FileSystem.get(config);
        if (fs.exists(outputPath)) {
            fs.delete(outputPath, true);
        }
        FileOutputFormat.setOutputPath(job, outputPath);
        // 提交作业
        boolean success = job.waitForCompletion(true);
        if (success) {
            System.out.println("执行成功");
        } else {
            System.out.println("执行失败");
        }
    }

}
